package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
import entidades.Processo;
import entidades.Vertice;

public class PanelGrafo extends javax.swing.JPanel {

	private VisualizationViewer<Vertice, String> view;
	private Dimension dimension;
	private StaticLayout staticLayout;
	
	
	public PanelGrafo() {
		super();
		staticLayout = new StaticLayout<Vertice, String>(Main.graph);
		initGUI();
	}
	
	public void initGUI() {
		try {
			setPreferredSize(new Dimension(825, 511));
			List<Vertice> mortos = new ArrayList<Vertice>();
			for (Vertice vertice : Main.graph.getVertices()) {
				if(vertice instanceof Processo){
					if(!((Processo)vertice).vivo){
						mortos.add(vertice);
					}
				}
			}
			
			for (Vertice vertice : mortos) {
				 Main.graph.removeVertex(vertice);
			}
			
			for (Vertice vertice : Main.graph.getVertices()) {
					staticLayout.setLocation(vertice,vertice.getPosX(),vertice.getPosY());
			}
			VisualizationViewer<Vertice, String> vv = new VisualizationViewer<Vertice, String>(staticLayout,new Dimension(825, 511));
			vv.getRenderContext().setVertexIconTransformer(new Transformer<Vertice, Icon>() {
				public Icon transform(Vertice vertice) {
					if(vertice instanceof Processo){
						if(((Processo) vertice).deadlock){
							return getImagem(1, "P", true);
						}else{
							return getImagem(1, "P", false);
						}
					}else{
						return getImagem(2, "R", false);
					}
				}
			});

			vv.getRenderContext().setArrowDrawPaintTransformer(new Transformer<String, Paint>() {
				public Paint transform(String edge) {
					return Color.BLACK;
				}
			});

			vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Vertice>());
			vv.getRenderer().getVertexLabelRenderer().setPosition(Position.S);
			add(vv);
			updateUI();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ImageIcon getImagem(final int tipo, final String label, final boolean deadlock){
		BufferedImage bf = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = bf.createGraphics(); 
		
		bf = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
		graphics = bf.createGraphics(); 
		
		if(tipo == 1){
			if(deadlock){
				graphics.setColor(Color.RED);
			}else{
				graphics.setColor(Color.BLUE);
			}
		}else{
			graphics.setColor(Color.YELLOW);
		}
		
		graphics.fill(new Rectangle(20,20));
		graphics.setColor(Color.WHITE);
		graphics.drawString(label,5, 18);
        
		return new ImageIcon(bf);
	}

}
